/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package AIproccess;

import Army.ComputerTeam;
import Army.PlayerTeam;
import Army.building.Building;
import Army.BuildingAndUnit;
import Army.building.BuildingBarrack;
import Army.building.BuildingHouse;
import Army.building.BuildingMine;
import Army.task.TaskBuild;
import GameObject.LayerManager;
import java.awt.Point;

/**
 *
 * @author Huy
 */
public class PlayerAIDevelopPro extends PlayerAIDevelope {

	//int nBarracks[] = new int[]{0,0,0,0,0,0};
	public PlayerAIDevelopPro(LayerManager layer, ComputerTeam team) {
		super(layer, team);
		//delay = 00;
	}

	@Override
	protected void proccess() {
		Building building = null;
		Point center = new Point(0, 0);

		int n = 0;
		for (int i = 0; i < layer.getListTeam().length; i++) {
			PlayerTeam foe = layer.getListTeam()[i];
			if (foe.isAlive()) {
				center.x += foe.getPosition().x;
				center.y += foe.getPosition().y;
				n++;
			}
		}
		center.x = center.x / n;
		center.y = center.y / n;
		if (team.getnBarrack() > 0 && team.getGold() > 500
				&& team.getCalledBuilding() != null
				&& team.getCalledBuilding().getType() == BuildingAndUnit.Type.BuildingCastle) {
			creatSoildier();
		} else if (team.getnBarrack() == 0 && team.getnWorker() == 0) {
			team.createNewUnit("worker", getCastle(team));
			return;
		} else if (team.getnBarrack() == 0
				&& team.getGold() > BuildingBarrack.BUILD_COST + 3 * BuildingMine.BUILD_COST
				|| (team.getnBarrack() < 2 && team.getGold() > 2 * BuildingBarrack.BUILD_COST)) {
			building = new BuildingBarrack(team.getTeamId());
		} else if (team.getPopulationLimit() < PlayerTeam.LIMIT
				&& team.getPopulation() > team.getPopulationLimit() - team.getnBarrack()) {
			building = new BuildingHouse(team.getTeamId());
		} else if (team.getPopulationLimit() / 2 < team.getPopulation() && team.getnBarrack() > 0 && team.getGold() < 500 && team.getnMine() < 20) {
			building = new BuildingMine(team.getTeamId());
		} else {
			if (team.getnBarrack() * 30 < team.getPopulation() && team.getGold() > BuildingBarrack.BUILD_COST) {
				building = new BuildingBarrack(team.getTeamId());
			} else if (team.getnBarrack() > 0 && team.getPopulationLimit() - team.getPopulation() > RANDOM.nextInt(3)) {
				creatSoildier();
			} else if (team.getnWorker() < RANDOM.nextInt(4) + team.getnBarrack()) {
				team.createNewUnit("worker", getCastle(team));
				return;
			}
			if (team.getnMine() < 10 || team.getnMine() < 10 * team.getnBarrack()) {
				building = new BuildingMine(team.getTeamId());
			} else if (team.getnBarrack() > 0 && team.LIMIT > team.getPopulationLimit()
					&& team.getPopulation() > team.getPopulationLimit() / 2) {
				building = new BuildingHouse(team.getTeamId());
			} else {
				creatSoildier();
			}

		}
		for (int i = 0; i < team.getListBuilding().size() - 1; i++) {
			Building b = team.getListBuilding().get(i);
			if (!b.isCompleted()) {
				BuildingAndUnit w = null;
				double d = Double.MAX_VALUE;
				for (int j = 0; j < team.getWorkerGroup().size(); j++) {
					BuildingAndUnit worker = team.getWorkerGroup().getListUnit().get(j);

					if (worker.isTaskCompleted()) {
						double newd = worker.getPosition().distance(b.getPosition());
						if (newd < d) {
							d = newd;
							w = worker;
						}
					}
				}
				if (w != null) {
					w.setTask(new TaskBuild(w, b));
					return;
				}
			}

		}
		BuildingAndUnit w = null;
		if (building != null && !team.getWorkerGroup().getListUnit().isEmpty()) {
			for (int j = 0; j < team.getWorkerGroup().size(); j++) {
				BuildingAndUnit worker = team.getWorkerGroup().getListUnit().get(j);

				if (worker.isTaskCompleted()) {
					if (building.getType() == BuildingAndUnit.Type.BuildingBarrack) {
						building.setPosition(getPosition(team.getCastle(), 200));
						if (team.getnBarrack() > 0) {
							for (int i = 0; i < 10; i++) {
								Point pos = getPosition(team.getCastle(), 200 + team.getnBarrack() * 100);
								if (pos.distance(center) < building.getPosition().distance(center)) {
									building.setPosition(pos);
								}
							}
						}

					} else {
						Building bb = null;
						double mind = Double.MAX_VALUE;
						for (int k = 0; k < team.getListBuilding().size() / 3 + 1; k++) {
							int i = (int) (Math.random() * team.getListBuilding().size());
							if (i != team.getListBuilding().size()) {
								Building b = team.getListBuilding().get(i);
								double d = worker.getPosition().distance(b.getPosition());
								if (building.getType() == building.getType()
										&& (d * RANDOM.nextDouble() < mind * RANDOM.nextDouble())) {
									mind = d;
									bb = b;
								}
							}
						}
						if (bb == null) {
							building.setPosition(getPosition(worker, 300));
						} else {
							building.setPosition(getPosition(bb, 200));
						}
					}
					w = worker;
					team.getWorkerGroup().getListUnit().remove(worker);
					team.getWorkerGroup().add(worker);
					break;
				}
			}
			if (w == null) {
				if (RANDOM.nextDouble() > 0.9999) {
					int i = RANDOM.nextInt(team.getWorkerGroup().size());
					w = team.getWorkerGroup().getListUnit().get(i);
					building.setPosition(getPosition(w, 200));
				}
			}
			if (w != null) {

				fixPosition(building);
				if (layer.isBuildAble(building)) {
					if (team.add(building)) {
						building.startBuild();
						w.setTask(new TaskBuild(w, building));
					}
				}
			}

		}
	}

	protected void creatSoildier() {
		Building barrack = getBarrack(team);
		if (barrack != null) {
			if (RANDOM.nextBoolean() == true) {
				team.createNewUnit("archer", barrack);
			} else {
				if (RANDOM.nextBoolean() == true) {
					team.createNewUnit("swordman", barrack);
				} else {
					team.createNewUnit("spearman", barrack);
				}
			}
		}
	}
}
